﻿package pk;

public class Test{ 

 public static void main(String[] args){ 

  Test  t = new Test(); 

  String email = "test@sina.com" ; 

  boolean  isEmail = t.isEmail(email); 

  if(isEmail){ 

   System.out.println(email +" is  a right email!!!"); 

  }else{ 

   System.out.println("error"); 

  } 

 } 

 public boolean isEmail(String email){ 

  if(email==null||"".equals(email)) return false ; 

  if(!containsOneWord('@',email)||!containsOneWord('.',email)) return false; 

  String prefix = email.substring(0,email.indexOf("@")); 

  String middle = email.substring(email.indexOf("@")+1,email.indexOf(".")); 

  String subfix = email.substring(email.indexOf(".")+1); 

  System.out.println("prefix="+prefix +"  middle="+middle+"  subfix="+subfix); 

    

  if(prefix==null||prefix.length()>40||prefix.length()==0) return false ; 

  if(!isAllWords(prefix)) return false ; 

  if(middle==null||middle.length()>40||middle.length()==0) return false ; 

  if(!isAllWordsAndNo(middle)) return false ; 

  if(subfix==null||subfix.length()>3||subfix.length()<2) return false ; 

  if(!isAllWords(subfix)) return false ; 

  return true ; 

 } 

 private boolean containsOneWord(char c , String word){ 

  char[] array = word.toCharArray(); 

  int count = 0 ; 

  for(Character ch : array){ 

   if(c == ch) { 

    count++; 

   } 

  } 

  return count==1 ; 

 } 
	private boolean isAllWords(String prefix){ 

  char[] array = prefix.toCharArray(); 

  for(Character ch : array){ 

   if(ch<'A' || ch>'z' || (ch<'a' && ch>'Z')) return false ; 

  } 

  return true; 

 } 
 private boolean isAllWordsAndNo(String middle){ 

  char[] array = middle.toCharArray(); 

  for(Character ch : array){ 

   if(ch<'0' || ch > 'z') return false ; 

   else if(ch >'9' && ch <'A') return false ; 

   else if(ch >'Z' && ch <'a') return false ; 

  } 

  return true ; 

 } 

}